﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using D2DEngine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using D2DEngine.Scene;
using D2DEngine.Core;
using D2DEngine.Components;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using D2DEngine.ParticleEffect;
using D2DEngine.Util;

namespace AIDev.Components
{
    public delegate void OnPlayerDeathDelegate();

    public class PlayerControlComponent : D2DComponent
    {
        D2DActor sceneObject;
        public OnPlayerDeathDelegate OnPlayerDeath;

        public override void OnRegister()
        {
            this.sceneObject = this.owner as D2DActor;

            this.sceneObject.Body.BodyType = BodyType.Dynamic;
            this.sceneObject.Body.OnCollision += this.OnCollision;
            this.sceneObject.Body.Mass = .1f;

            this.sceneObject.Body.CollisionCategories = Category.Cat5;

            // Save the scene object inside the body
            this.sceneObject.Body.UserData = this.sceneObject;
            this.sceneObject.Body.LinearDamping = 2.0f;
            this.sceneObject.Body.Restitution = 0.0f;

            base.OnRegister();
        }

        public override void Update(GameTime gameTime)
        {
            D2DInputManager input = D2DInputManager.Instance;
            Vector2 direction = Vector2.Zero;

            if (input.IsKeyDown(Keys.W))
            {
                this.sceneObject.Body.ApplyLinearImpulse(new Vector2(0, -0.005f));
            }

            if (input.IsKeyDown(Keys.S))
            {
                this.sceneObject.Body.ApplyLinearImpulse(new Vector2(0, 0.005f));
            }

            if (input.IsKeyDown(Keys.D))
            {
                this.sceneObject.Body.ApplyLinearImpulse(new Vector2(0.005f, 0f));
            }

            if (input.IsKeyDown(Keys.A))
            {
                this.sceneObject.Body.ApplyLinearImpulse(new Vector2(-0.005f, 0f));
            }

            if (input.IsKeyPressed(Keys.Space))
            {
                FireProjectile();
            }

            base.Update(gameTime);
        }


        private void FireProjectile()
        {
            D2DActor bullet = D2DObjectDatabase.Instance.CloneObject<D2DActor>("Projectile_missile");
            bullet.Position = this.sceneObject.Position;
            bullet.Rotation = this.sceneObject.Rotation;
            bullet.Body.CollisionCategories = Category.Cat6;
            bullet.Body.CollidesWith = Category.All & ~Category.Cat5;

            this.sceneObject.Scene.Register(bullet);
        }


        /// <summary>
        /// When the player dies
        /// </summary>
        public override void OnUnRegister()
        {
           // D2DEventManager.Instance.RaiseEvent("PlayerDeath");

            this.OnPlayerDeath();

            base.OnUnRegister();
        }


        public bool OnCollision(Fixture us, Fixture them, Contact contact)
        {
            if (them.Body.UserData != null && them.Body.UserData is D2DActor)
            {
                D2DActor actor = (D2DActor)them.Body.UserData;

                actor.Dispose();
                this.sceneObject.Dispose();


                D2DParticleEffect effect = this.sceneObject.Scene.FindObject<D2DParticleEffect>("Effect_explosionFire");
                effect.Position = D2DMath.ToPixels(this.sceneObject.Position);
                effect.Spawn();

                return false;
            }

            return true;
        }

        protected override void CopyTo(D2DComponent component)
        {
            base.CopyTo(component);

            PlayerControlComponent comp = (PlayerControlComponent)component;
            comp.OnPlayerDeath = this.OnPlayerDeath;
        }
    }
}
